﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleApp.MvcBackend.ComponentModel
{
	public class Commit : ICommit
	{
		public static Commit CreateFor( Guid transactionIdentifier, IAggregate aggregate )
		{
			return new Commit( transactionIdentifier, aggregate.Id, aggregate.GetUncommittedEvents() );
		}

		[Raven.Imports.Newtonsoft.Json.JsonConstructor]
		private Commit()
		{

		}

		private Commit( Guid transactionIdentifier, String aggregateId, IEnumerable<IDomainEvent> uncommittedEvents )
		{
			this.Id = String.Format( "commits/{0}", Guid.NewGuid() );
			this.CreatedOn = DateTimeOffset.Now;
			this.TransactionIdentifier = transactionIdentifier.ToString();
			this.AggregateIdentifier = aggregateId;
			this.Events = uncommittedEvents;
		}

		public String Id { get; private set; }
		public DateTimeOffset CreatedOn { get; private set; }
		public String TransactionIdentifier { get; private set; }
		public String AggregateIdentifier { get; private set; }
		public IEnumerable<IDomainEvent> Events { get; private set; }

		public Boolean IsDispatched { get; private set; }
	}
}
